package org.linzhu.Connect4.ui;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import org.linzhu.Connect4.core.Player;

/**
 *
 * @author zhulin
 */
public class Connect4CellRenderer extends JLabel implements TableCellRenderer {

    private static final Color player1Color = new Color(255, 255, 255); // WHITE
    private static final Color player2Color = new Color(0, 0, 0); // BLACK
    private static final Color defaultColor = new Color(0, 0, 128); // NAVY BLUE

    public Connect4CellRenderer() {
        setOpaque(true); // MUST do this for background to show up.
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        if (value != null) {
            if (value == Player.Player1) {
                setBackground(player1Color);
            } else if (value == Player.Player2) {
                setBackground(player2Color);
            } else {
                throw new IllegalArgumentException("Unknown value " + value + " [" + value.getClass() + "]");
            }
        } else {
            setBackground(defaultColor);
        }
        return this;
    }
}
